import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/utils/input_formatter_tools.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';

class NormalTableProAgeHeader extends StatelessWidget {
  final List<String> items;
  final EdgeInsets padding;
  final bool hasDivider;
  final List<int> widthList;

  const NormalTableProAgeHeader(
      {Key key,
      this.items,
      this.padding,
      this.hasDivider = true,
      this.widthList})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return NormalTableProDefaultItem(
      style: TextStyle(
        color: aceDarkGrayColor50,
        fontSize: getSp(14),
        fontWeight: FontWeight.w700,
        height: 22 / 14,
        fontFamily: "DinRoundPro",
      ),
      items: items,
      padding: padding,
      hasDivider: hasDivider,
      widthList: widthList,
    );
  }
}

class NormalTableProHeader extends StatelessWidget {
  final List<String> items;
  final List<int> widthList;

  const NormalTableProHeader({Key key, this.items, this.widthList})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return NormalTableProDefaultItem(
      style: TextStyle(
        color: aceBlackColor,
        fontSize: getSp(16),
        fontWeight: FontWeight.w700,
        height: 22 / 16,
      ),
      items: items,
      widthList: widthList,
    );
  }
}

class NormalTableProTitleNoBackItem4 extends StatelessWidget {
  final List<String> items;
  final bool isEndCenter;
  final bool isStartCenter;
  final EdgeInsets padding;
  final bool hasDivider;
  final List<int> widthList;
  const NormalTableProTitleNoBackItem4(
      {Key key,
      this.items,
      this.isStartCenter = false,
      this.isEndCenter = false,
      this.hasDivider = true,
      this.padding,
      this.widthList})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return NormalTableProDefaultItem(
      style: TextStyle(
        color: aceDarkGrayColor,
        fontSize: getSp(16),
        fontWeight: FontWeight.w700,
        height: 22 / 16,
      ),
      items: items,
      isEndCenter: isEndCenter,
      isStartCenter: isStartCenter,
      padding: padding,
      hasDivider: hasDivider,
      widthList: widthList,
    );
  }
}

class NormalTableProTitleNoBackItem extends StatelessWidget {
  final String title;
  final String value;
  final EdgeInsets padding;

  const NormalTableProTitleNoBackItem(
      {Key key, this.title, this.value, this.padding})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: padding ??
          EdgeInsets.only(
            left: getDp(35),
            top: getDp(20),
            right: getDp(35),
            bottom: getDp(15),
          ),
      child: DefaultTextStyle(
        style: TextStyle(
          color: aceDarkGrayColor,
          fontSize: getSp(16),
          fontWeight: FontWeight.w700,
          height: 22 / 16,
          fontFamily: "DinRoundPro",
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text('$title'),
            if (value != null) Text('$value'),
          ],
        ),
      ),
    );
  }
}

class NormalTableProTitleItem extends StatelessWidget {
  final String title;
  final String value;
  final EdgeInsets padding;
  final ValueChanged<String> onUpdateValue;

  const NormalTableProTitleItem(
      {Key key, this.title, this.value, this.padding, this.onUpdateValue})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: padding ??
          EdgeInsets.only(
            left: getDp(35),
            top: getDp(5),
            right: getDp(35),
            bottom: getDp(5),
          ),
      color: aceWhiteColor,
      child: DefaultTextStyle(
        style: TextStyle(
          color: aceDarkGrayColor,
          fontSize: getSp(16),
          fontWeight: FontWeight.w700,
          height: 22 / 16,
          fontFamily: "DinRoundPro",
        ),
        child: Row(
          children: [
            Expanded(
                child: Text(
              '${title.replaceAll('\\n', '\n')}',
              // maxLines: 1,
              overflow: TextOverflow.ellipsis,
            )),
            if (value != null)
              Padding(
                padding: const EdgeInsets.only(left: 15.0),
                child: NormalTableEditItem(
                  onUpdateValue: onUpdateValue,
                  initValue: value,
                ),
              ),
          ],
        ),
      ),
    );
  }
}

class NormalTableProSubTitleNormalItem extends StatelessWidget {
  final String title;
  final String value;
  final ValueChanged<String> onUpdateValue;

  const NormalTableProSubTitleNormalItem(
      {Key key, this.title, this.value, this.onUpdateValue})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        left: getDp(35),
        top: getDp(15),
        right: getDp(35),
      ),
      child: DefaultTextStyle(
        style: TextStyle(
          color: aceBlackColor,
          fontSize: getSp(16),
          fontWeight: FontWeight.w400,
          height: 22 / 16,
          fontFamily: "DinRoundPro",
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: HtmlWidget(
                '$title',
              ),
            ),
            if (value != null)
              NormalTableEditItem(
                onUpdateValue: onUpdateValue,
                initValue: value,
              ),
          ],
        ),
      ),
    );
  }
}

class NormalTableProSubTitleItem extends StatelessWidget {
  final String title;
  final String value;
  final EdgeInsets padding;
  final ValueChanged<String> onUpdateValue;

  const NormalTableProSubTitleItem(
      {Key key, this.padding, this.title, this.value, this.onUpdateValue})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: padding ??
          EdgeInsets.only(
            left: getDp(26),
            top: getDp(20),
            right: getDp(26),
            bottom: getDp(15),
          ),
      child: DefaultTextStyle(
        style: TextStyle(
          color: aceBlackColor,
          fontSize: getSp(16),
          fontWeight: FontWeight.w700,
          height: 22 / 16,
          fontFamily: "DinRoundPro",
        ),
        child: Row(
          children: [
            Expanded(
              child: HtmlWidget(
                '$title',
              ),
            ),
            if (value != null)
              NormalTableEditItem(
                onUpdateValue: onUpdateValue,
                initValue: value,
              )
          ],
        ),
      ),
    );
  }
}

class NormalTableProHighLightItem extends StatelessWidget {
  final List<String> items;
  final bool isEndCenter;
  final bool isStartCenter;
  final EdgeInsets padding;
  final bool hasDivider;

  const NormalTableProHighLightItem(
      {Key key,
      this.items,
      this.isStartCenter = false,
      this.isEndCenter = false,
      this.hasDivider,
      this.padding})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return NormalTableProDefaultItem(
      style: TextStyle(
        color: aceBlackColor,
        fontSize: getSp(16),
        fontWeight: FontWeight.w700,
        height: 22 / 16,
      ),
      items: items,
      isEndCenter: isEndCenter,
      isStartCenter: isStartCenter,
      padding: padding,
      hasDivider: hasDivider,
    );
  }
}

class NormalTableProItem extends StatelessWidget {
  final List<String> items;
  final bool isEndCenter;
  final bool isStartCenter;
  final EdgeInsets padding;
  final bool hasDivider;
  final ValueChanged<String> onUpdateValue;
  final List<int> widthList;

  const NormalTableProItem(
      {Key key,
      this.items,
      this.isStartCenter = false,
      this.isEndCenter = false,
      this.hasDivider = true,
      this.padding,
      this.onUpdateValue,
      this.widthList})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return NormalTableProDefaultItem(
      style: TextStyle(
        fontSize: getSp(16),
        color: aceBlackColor,
        fontWeight: FontWeight.w400,
        height: 22 / 16,
      ),
      onUpdateValue: onUpdateValue,
      items: items,
      isEndCenter: isEndCenter,
      isStartCenter: isStartCenter,
      padding: padding,
      hasDivider: hasDivider,
      widthList: widthList,
    );
  }
}

class NormalTableProDefaultItem extends StatelessWidget {
  final List<String> items;
  final TextStyle style;
  final bool isEndCenter;
  final bool isStartCenter;
  final EdgeInsets padding;
  final bool hasDivider;
  final ValueChanged<String> onUpdateValue;
  final List<int> widthList;

  const NormalTableProDefaultItem(
      {Key key,
      this.items,
      this.padding,
      this.style,
      this.hasDivider = true,
      this.isEndCenter = false,
      this.isStartCenter,
      this.onUpdateValue,
      this.widthList})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: padding ??
          EdgeInsets.only(
            left: getDp(35),
            top: getDp(15),
            right: getDp(35),
            bottom: getDp(20),
          ),
      decoration: BoxDecoration(
        color: Colors.white,
        // border: hasDivider == true
        //     ? Border(
        //         bottom: BorderSide(
        //         width: getDp(0.5),
        //         color: aceGrayColor,
        //       ))
        //     : null
      ),
      child: DefaultTextStyle(
        style: style.copyWith(
          fontFamily: "DinRoundPro",
        ),
        child: Row(
          children: List.generate(items.length, (index) {
            final title = items[index];
            String textAlign = 'center';
            Alignment alignment = Alignment.topCenter;
            if (index == 0) {
              if (isStartCenter != true) {
                textAlign = 'left';
                alignment = Alignment.centerLeft;
              }
            } else if (index == items.length - 1) {
              if (isEndCenter != true) {
                textAlign = 'right';
                alignment = Alignment.centerRight;
              }
              if (onUpdateValue != null) {
                return NormalTableEditItem(
                  initValue: title,
                  onUpdateValue: onUpdateValue,
                );
              }
            }
            return Expanded(
              flex: widthList != null ? widthList[index] : 1,
              child: Container(
                alignment: alignment,
                child: HtmlWidget(
                  '<div style="text-align:$textAlign">${title?.replaceAll(r'\n', '\n')}</div>',
                ),
              ),
            );
          }),
        ),
      ),
    );
  }
}

class NormalTableEditItem extends StatelessWidget {
  final String initValue;
  final ValueChanged<String> onUpdateValue;

  const NormalTableEditItem({Key key, this.initValue, this.onUpdateValue})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return onUpdateValue != null
        ? Container(
            constraints: BoxConstraints(
              maxWidth: getDp(120),
            ),
            child: TextFormField(
              initialValue: initValue,
              inputFormatters: [
                doubleLimit2InputFormatter,
              ],
              onChanged: (value) {
                onUpdateValue?.call(value);
              },
              scrollPadding: EdgeInsets.zero,
              obscureText: false,
              keyboardType: TextInputType.numberWithOptions(decimal: true),
              enabled: FormEditStatusInheritedWidget.ofEnable(context),
              style: TextStyle(
                  fontSize: getSp(16),
                  color: aceBlueColor,
                  fontWeight: FontWeight.w700,
                  height: 14 / 16),
              textAlign: TextAlign.right,
              enableSuggestions: true,
              autofillHints: [
                AutofillHints.email,
                AutofillHints.name,
                AutofillHints.countryCode,
                AutofillHints.gender,
                AutofillHints.nickname,
                AutofillHints.jobTitle,
              ],
              decoration: InputDecoration(
                border: InputBorder.none,
                focusedBorder: InputBorder.none,
                disabledBorder: InputBorder.none,
              ),
            ),
          )
        : HtmlWidget('$initValue');
  }
}
